package com.huijie.app.u.shiro;

import com.huijie.app.u.shiro.impl.AccountProviderImpl;
import com.huijie.app.u.shiro.impl.FilteRulesProviderImpl;
import com.huijie.app.u.shiro.impl.PasswdRetryLimitHandlerImpl;
import com.huijie.app.u.shiro.impl.StatelessAccountProviderImpl;
import com.huijie.core.constant.ResponseCode;
import org.jsets.shiro.config.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

/** 
* @ClassName: ApplicationSecurityConfig
* @Description: 系统安全配置 继承自JsetsShiroConfigurationAdapter以扩展、定制鉴权组件
* @author Alex
* @date 2018年6月29日 下午5:48:03
*/
@Configuration
public class ApplicationSecurityConfig extends JsetsShiroConfigurationAdapter {

	// 账号信息提供者
	@Autowired
	private AccountProviderImpl accountProviderImpl;
	// 密码输入错误次数超限处理器
	@Autowired
	private PasswdRetryLimitHandlerImpl passwdRetryLimitHandlerImpl;
	// 动态URL过滤规则
	@Autowired
	private FilteRulesProviderImpl filteRulesProviderImpl;
	// 无状态账号信息提供者
	@Autowired
	private StatelessAccountProviderImpl statelessAccountProviderImpl;
	
	@Override
	protected void configure(SecurityManagerConfig securityManager) {
		securityManager.setAccountProvider(accountProviderImpl);
		securityManager.setPasswdRetryLimitHandler(passwdRetryLimitHandlerImpl);
		securityManager.setStatelessAccountProvider(statelessAccountProviderImpl);

		//错误码初始化
		MessageConfig.UNKONWN_REASON = new ResponseCodeMessage(ResponseCode.UNKONWN_REASON.getCode(),ResponseCode.UNKONWN_REASON.getMessage());
		MessageConfig.TOKEN_UNAUTHORIZED = new ResponseCodeMessage(ResponseCode.TOKEN_UNAUTHORIZED.getCode(),ResponseCode.TOKEN_UNAUTHORIZED.getMessage());
		MessageConfig.JWT_TOKEN_IS_MISSING = new ResponseCodeMessage(ResponseCode.JWT_TOKEN_IS_MISSING.getCode(),ResponseCode.JWT_TOKEN_IS_MISSING.getMessage());
		MessageConfig.INVALID_CREDENCIAL = new ResponseCodeMessage(ResponseCode.INVALID_CREDENCIAL1.getCode(),ResponseCode.INVALID_CREDENCIAL1.getMessage());
	}

	@Override
	protected void configure(FilterChainConfig filterChain) {
		filterChain.setShiroFilteRulesProvider(filteRulesProviderImpl);
	}

}